package leetcode.newVersion.Hash;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

public class lc49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<HashMap<Character, Integer>, List<String>> stringMap = new HashMap<>();

        for (int i = 0; i < strs.length; i++) {
            HashMap<Character, Integer> charNumberMap = new HashMap<>();
            for (int j = 0; j < strs[i].length(); j++) {//遍历每个元素的每个字节，添加到map中，记录出现的次数
                charNumberMap.put(strs[i].charAt(j), charNumberMap.getOrDefault(strs[i].charAt(j), 0) + 1);
            }
            //如果map中不存在对应的这个key，那么就创建，然后操作map
            stringMap.computeIfAbsent(charNumberMap, k -> new ArrayList<>()).add(strs[i]);
        }

        //将 Map 中所有的值（List<String>）提取出来，放入一个新的 ArrayList 中，
        List<List<String>> res = new ArrayList<>(stringMap.values());
        return res;
    }

    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        lc49 solution = new lc49();
        List<List<String>> result = solution.groupAnagrams(strs);
        System.out.println(result);
    }
}
